﻿Imports ControllerSigeco

Public Class frmMarca

    Private Sub frmMarca_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgMarcas.AutoGenerateColumns = False
        CargarMarcas()
    End Sub

    Dim classController As New controller_stock

    Private _idMarca As Integer = 0

    Public Property idMarca() As Integer
        Get
            Return _idMarca
        End Get
        Set(value As Integer)
            _idMarca = value
        End Set
    End Property

    Private Sub CargarMarca()
        Try
            If idMarca > 0 Then

                Dim ds As New DataTable
                ds = classController.get_marcas(oiml.dbConn, idMarca, "")

                If Not ds Is Nothing Then

                    txtMarca.Text = ds.Rows(0)("marca")
                    btnGrabar.Enabled = True
                    btnEliminar.Enabled = True
                    btnBuscar.Enabled = False

                End If

            Else
                txtMarca.Text = ""
                txtMarca.Focus()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub CargarMarcas()
        Try
            idMarca = 0
            txtMarca.Focus()

            btnBuscar.Enabled = True
            btnNuevo.Enabled = True
            btnEliminar.Enabled = True
            btnGrabar.Enabled = False

            Dim ds As New DataTable
            ds = classController.get_marcas(oiml.dbConn, 0, txtMarca.Text)

            If Not ds Is Nothing Then
                dgMarcas.DataSource = ds
            Else
                dgMarcas.DataSource = Nothing
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs) Handles btnEliminar.Click

        If idMarca > 0 Then
            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Marcas", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Delete_marcas(oiml.dbConn, idMarca) Then
                    txtMarca.Text = ""
                    CargarMarcas()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Marcas", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtMarca.Text = "" Then
            ErrorProvider1.SetError(txtMarca, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtMarca, "")
        End If        '

        Return controlCampos

    End Function

    Private Sub btnGrabar_Click(sender As System.Object, e As System.EventArgs) Handles btnGrabar.Click

        If ValidarCampos() Then
            Exit Sub
        End If

        If idMarca = 0 Then
            idMarca = classController.Insert_marcas(oiml.dbConn, txtMarca.Text)
            If idMarca > 0 Then
                txtMarca.Text = ""
                CargarMarcas()
                MessageBox.Show("Registro grabado con exito!", "Marcas", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Marcas", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If classController.Update_marca(oiml.dbConn, idMarca, txtMarca.Text) Then
                txtMarca.Text = ""
                CargarMarcas()
                MessageBox.Show("Registro grabado con exito!", "Marcas", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)


            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Marcas", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub

    Private Sub editar()
        Try

            If dgMarcas.SelectedRows.Count > 0 Then

                idMarca = dgMarcas.SelectedRows(0).Cells(0).Value
                CargarMarca()

            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnNuevo_Click(sender As System.Object, e As System.EventArgs) Handles btnNuevo.Click
        btnEliminar.Enabled = False
        btnNuevo.Enabled = False
        btnBuscar.Enabled = False
        btnGrabar.Enabled = True

        idMarca = 0
        txtMarca.Text = ""
        txtMarca.Focus()

    End Sub

    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        CargarMarcas()

    End Sub

    Private Sub dgMarcas_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgMarcas.KeyDown
        If e.KeyCode = Keys.Enter Then
            editar()
        End If
    End Sub

    Private Sub dgMarcas_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgMarcas.DoubleClick
        editar()
    End Sub

    Private Sub txtMarca_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtMarca.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMarcas()
        End If
    End Sub

    Private Sub dgMarcas_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgMarcas.CellClick
        Try

            If dgMarcas.SelectedRows.Count > 0 Then

                idMarca = dgMarcas.SelectedRows(0).Cells(0).Value


            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class